import Vue from 'vue';
import Router from 'vue-router';
import Login from '@/../src/views/UserLogin.vue';
import FileUpload from '@/../src/views/FileUpload.vue';
import FileList from '@/../src/views/FileList.vue';
import NotFound from '@/../src/views/NotFound.vue';
Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login
    },
    {
      path: '/upload',
      name: 'FileUpload',
      component: FileUpload,
      meta: { requiresAuth: true }
    },
    {
      path: '/list',
      name: 'FileList',
      component: FileList,
      meta: { requiresAuth: true }
    },
    {
      path: '*',
      name: 'NotFound',
      component: NotFound
    }
  ]
});

router.beforeEach((to, from, next) => {
  // 检查用户是否已登录
  const loggedIn = localStorage.getItem('user');
  
  // 如果目标路由需要身份验证且用户未登录
  if (to.matched.some(record => record.meta.requiresAuth) && !loggedIn) {
    // 重定向到首页
    next('/');
  } else {
    // 继续导航
    next();
  }
});

export default router;